package com.huawei.crm.web.controller;

import com.huawei.crm.domain.Customer;
import com.huawei.crm.domain.Employee;
import com.huawei.crm.query.CustomerQueryObject;
import com.huawei.crm.service.ICustomerService;
import com.huawei.crm.service.IEmployeeService;
import com.huawei.crm.service.ISystemdictionaryitemService;
import com.huawei.crm.util.JsonResult;
import com.huawei.crm.util.UserContext;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService service;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private ISystemdictionaryitemService systemdictionaryitemService;

    // value: 指定当前方法需要的权限
    // logical: 设置当前用户是否是必须拥有所有的权限才能访问当前的方法
    // AND: 同时拥有,默认值
    // OR: 拥有其中任何一个即可
    @RequestMapping("/potentialList")
    @RequiresPermissions(value = {"潜在客户列表","customer:list"}, logical = Logical.OR)
    public String list(Model model,@ModelAttribute("qo") CustomerQueryObject qo) {
        qo.setStatus(Customer.STATUS_POTENTIAL);

        // 获取当前用户
        Employee currentEmp = UserContext.getCurrentEmp();
        // 判断当前用户是否是超管或经理
        if(!(currentEmp.isAdmin() || SecurityUtils.getSubject().hasRole("Market_Manager"))){
            qo.setSellerId(currentEmp.getId());
        }
        model.addAttribute("pageInfo", service.query(qo));
        // 查询所有的营销人员
        List<Employee> emps = employeeService.listEmpsByRoleSns("Market_Manager","Market");
        model.addAttribute("sellers",emps);

        // 查询所有的工作
        model.addAttribute("jobs",systemdictionaryitemService.listItemsByDictionarySn("job"));
        // 查询所有的来源
        model.addAttribute("sources",systemdictionaryitemService.listItemsByDictionarySn("source"));
        // 查询跟进方式
        model.addAttribute("types",systemdictionaryitemService.listItemsByDictionarySn("communicationMethod"));

        return "customer/list";
    }

    @RequestMapping("/poolList")
    @RequiresPermissions(value = {"客户池列表", "customer:poolList"}, logical = Logical.OR)
    public String poolList(Model model, @ModelAttribute("qo") CustomerQueryObject qo) {
        qo.setStatus(Customer.STATUS_POOL);
        model.addAttribute("pageInfo",service.query(qo));

        //查询所有的营销人员
        List<Employee> emps = employeeService.listEmpsByRoleSns("Market_Manager", "Market");
        model.addAttribute("sellers", emps);
        return "customer/poolList";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"潜在客户保存或修改","customer:saveOrUpdate"}, logical = Logical.OR)
    @ResponseBody // 将返回值转换成json格式的字符串并响应给页面
    public JsonResult saveOrUpdate(Customer c) {
        JsonResult result = new JsonResult();
        try {
            service.saveOrUpdate(c);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("亲,操作失败!");
        }
        return result;
    }

    @RequestMapping("/updateStatus")
    @RequiresPermissions(value = {"潜在客户状态修改","customer:updateStatus"}, logical = Logical.OR)
    @ResponseBody // 将返回值转换成json格式的字符串并响应给页面
    public JsonResult updateStatus(Long customerId, Integer status) {
        JsonResult result = new JsonResult();
        try {
            service.updateStatus(customerId, status);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("亲,操作失败!");
        }
        return result;
    }
}
